package com.engine.kq.cmd.attendanceEvent;


import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQGroupMemberComInfo;
import com.engine.kq.biz.KQLeaveRulesBiz;
import com.engine.kq.enums.DurationTypeEnum;
import com.engine.kq.util.KQDurationCalculatorUtil;
import java.util.HashMap;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.systeminfo.SystemEnv;
/**
 * 请假用的时长计算
 */
public class GetLeaveWorkDurationCmd extends AbstractCommonCommand<Map<String, Object>> {

	public GetLeaveWorkDurationCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		BaseBean bb=new BaseBean();
		Map<String, Object> retmap = new HashMap<String, Object>();
		RecordSet rs = new RecordSet();
		String sql = "";
		try{
      String newLeaveType = Util.null2String(params.get("newLeaveType"));
			bb.writeLog("当前请假类型"+newLeaveType);
      String resourceId = Util.null2String(params.get("resourceId"));
			bb.writeLog("当前resourceId:"+resourceId);
      String fromDate = Util.null2String(params.get("fromDate"));
			bb.writeLog("当前fromDate:"+fromDate);
      String toDate = Util.null2String(params.get("toDate"));
			bb.writeLog("当前toDate:"+toDate);
      String fromTime = Util.null2String(params.get("fromTime"));
			bb.writeLog("当前fromTime:"+fromTime);
      String toTime = Util.null2String(params.get("toTime"));
			bb.writeLog("当前toTime:"+toTime);

      ResourceComInfo rci = new ResourceComInfo();
      KQGroupMemberComInfo kqGroupMemberComInfo = new KQGroupMemberComInfo();
      String groupid = kqGroupMemberComInfo.getKQGroupId(resourceId,fromDate);
      if(resourceId.length() > 0 && groupid.length() == 0){
        retmap.put("status", "-1");
        retmap.put("message",  rci.getLastname(resourceId)+","+fromDate+"没有设置考勤组，无法计算时长");
        return retmap;
      }

      String durationrule = Util.null2String(KQLeaveRulesBiz.getMinimumUnit(newLeaveType));
      String computingMode = Util.null2String(KQLeaveRulesBiz.getComputingMode(newLeaveType));

      KQDurationCalculatorUtil kqDurationCalculatorUtil =new KQDurationCalculatorUtil.DurationParamBuilder(resourceId).
          fromDateParam(fromDate).toDateParam(toDate).fromTimeParam(fromTime).toTimeParam(toTime).
          newLeaveTypeParam(newLeaveType).durationRuleParam(durationrule).
          computingModeParam(computingMode).durationTypeEnumParam(DurationTypeEnum.LEAVE).build();

      Map<String,Object> durationMap = kqDurationCalculatorUtil.getWorkDuration();

      retmap.put("duration", Util.null2String(durationMap.get("duration")));
      retmap.put("min_duration", Util.null2String(durationMap.get("min_duration")));
			retmap.put("status", "1");
		}catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(e);
		}
		bb.writeLog("测试2duration:"+retmap.get("duration"));
		bb.writeLog("测试2min_duration:"+retmap.get("min_duration"));
		return retmap;
	}

	@Override
	public BizLogContext getLogContext() {
		return null;
	}

}
